package org.vectrics.site.hosted;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionForm;

public class HostedApplicationForm extends VectricsActionForm {

	private static final long serialVersionUID = -5565063367021124990L;
	
	private transient Logger log = Logger.getLogger(this.getClass());
	public static int MINIMUM_PASSWORD_SIZE = 6; 	
	private String firstName = null;
	private String lastName = null;
	private String country = null;
	private String phoneNumber = null;
	private String email = null;
	private String emailFormat = null;
	private String workPhone = null;
	private String companyName = null;
	private String password = null;
	private String password2 = null;
	private String sectorCode = null;
	
	
	public HostedApplicationForm() {
		super();
	}
	
	public ActionErrors doValidate(ActionMapping mapping, Request request) {
		log.debug("validate() called");
		ActionErrors errors = new ActionErrors();
		if (this.wasButtonPressed(request, "cancel") == false) {
			if ((this.companyName == null) || (this.companyName.length() == 0)) {
				errors.add("companyName", new ActionMessage("error.company.name.required"));			
			} else {
				
			}
			
			if ((getEmail() == null) || (getEmail().trim().length() <= 0)) {
				log.debug("E-mail not given");
				errors.add("email", new ActionMessage("error.email.required"));			
			} else {
				/*
				// Email given - must be unique
				
				boolean emailInUse = CoreServiceLocator.getPersonService().emailInUse(ThreadProperties, 
						getEmail(), null);
				if (emailInUse) {
					errors.add("email", new ActionError("error.email.in.use"));
				}
				*/
			}
					
			if ((getPassword() == null) || (getPassword().trim().length() <= 0)) {
				errors.add("password", new ActionMessage("error.password.required"));			
			}
			else if ((getPassword() != null) && (getPassword().length() < MINIMUM_PASSWORD_SIZE)) {
				log.debug("validate() - password too short");
				errors.add("password", new ActionMessage("error.password.tooshort"));
				//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.tooshort"));			
			}
			
			if ((getPassword2() == null) || (getPassword2().trim().length() <= 0)) {
				errors.add("password2", new ActionMessage("error.password2.required"));			
			}
			else if ((getPassword2() != null) && (getPassword2().length() < MINIMUM_PASSWORD_SIZE)) {
				errors.add("password2", new ActionMessage("error.password2.tooshort"));
				//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password2.tooshort"));			
			}
			
			if ((getPassword() != null) && (getPassword().equals(getPassword2()) == false)) {
				errors.add("password", new ActionMessage("error.password.match"));
				
			}
			if ((this.getLastName() == null) || (this.getLastName().trim().length() == 0)) {
				errors.add("lastname", new ActionMessage("error.lastname.required"));
			}
			if ((this.getFirstName() == null) || (this.getFirstName().trim().length() == 0)) {
				errors.add("firstname", new ActionMessage("error.firstname.required"));
			}

		}		
		return(errors);	
	}

	/**
	 * @return Returns the companyName.
	 */
	public String getCompanyName() {
		return companyName;
	}

	/**
	 * @param companyName The companyName to set.
	 */
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	/**
	 * @return Returns the country.
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @param country The country to set.
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/**
	 * @return Returns the email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email The email to set.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return Returns the emailFormat.
	 */
	public String getEmailFormat() {
		return emailFormat;
	}

	/**
	 * @param emailFormat The emailFormat to set.
	 */
	public void setEmailFormat(String emailFormat) {
		this.emailFormat = emailFormat;
	}

	/**
	 * @return Returns the firstName.
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName The firstName to set.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return Returns the lastName.
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName The lastName to set.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return Returns the phoneNumber.
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber The phoneNumber to set.
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * @return Returns the workPhone.
	 */
	public String getWorkPhone() {
		return workPhone;
	}

	/**
	 * @param workPhone The workPhone to set.
	 */
	public void setWorkPhone(String workPhone) {
		this.workPhone = workPhone;
	}

	/**
	 * @return Returns the password.
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password The password to set.
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return Returns the password2.
	 */
	public String getPassword2() {
		return password2;
	}

	/**
	 * @param password2 The password2 to set.
	 */
	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	/**
	 * Return the choosen sector code (corresponds to sector object)
	 * @return
	 */
	public String getSectorCode() {
		return sectorCode;
	}

	public void setSectorCode(String sectorCode) {
		this.sectorCode = sectorCode;
	}

}
